package com.example.flutter_demo2.plugins.common

import android.content.Intent
import com.example.flutter_demo2.MyApplication.Companion.splashScreen
import com.example.flutter_demo2.aty.NativeActivity
import com.example.flutter_demo2.aty.OnSwitch2Web
import com.example.flutter_demo2.plugins.BasePlugin
import com.example.flutter_demo2.plugins.CHANNEL_EVENT
import com.example.flutter_demo2.plugins.CHANNEL_METHOD
import com.example.flutter_demo2.utils.CommonUtil
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel

const val channelNameCommon = "common"
const val methodNameFinishSplash = "finishSplash"
const val methodNameOpenNativePage = "openNativePage"
const val methodNameOpenWebView = "openWebView"

class CommonPlugin :
    BasePlugin(
        "$CHANNEL_METHOD/$channelNameCommon",
        eventChannelName = "$CHANNEL_EVENT/$channelNameCommon",
    ) {
    private var mOnSwitch2Web: OnSwitch2Web? = null


    fun setSwitch2WebCallback(onSwitch2Web: OnSwitch2Web) {
        mOnSwitch2Web = onSwitch2Web
    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        when (call.method) {
            methodNameFinishSplash -> {
                splashScreen.setKeepOnScreenCondition { false }
                if (isActivityInitialized) CommonUtil.cancelFullScreen(mRootActivity)
            }

            methodNameOpenNativePage -> {
                mRootActivity.startActivity(Intent(mRootActivity, NativeActivity::class.java))
            }

            methodNameOpenWebView -> {
                mOnSwitch2Web?.onSwitch2Web()
            }
        }
    }
}